<?php

/*
 * 数据对象映射模式 
 * 是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作
 * 
 */

namespace IMooc;

class User {

    public $id;
    public $name;
    public $mobile;
    public $regtime;
    protected $_db;

    function __construct($id)
    {
        $this->_db = new \IMooc\Database\MySQLi();
        $this->_db->connect('127.0.0.1', 'root', '', 'design-patterns');
        $id = (int) $id;
        $res = $this->_db->query('select * from user where id = ' . $id);
        $data = $res->fetch_assoc();
        $this->id = $id;
        $this->name = $data['name'];
        $this->mobile = $data['mobile'];
        $this->regtime = $data['regtime'];
    }

    function __destruct()
    {
        $this->_db->query("update user set name = '{$this->name}', mobile = '{$this->mobile}', regtime = '{$this->regtime}' where id = {$this->id}");
    }

}
